package edu.luc.concordia;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.event.EventListenerList;

/**
 * Copyright 2008 Project Concordia
 * <p>
 * This file is part of RetailReturnSystem.
 * <p>
 * RetailReturnSystem is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * <p>
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * <p>
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <a href="http://www.gnu.org/licenses/">http://www.gnu.org/licenses/</a>.
 * <hr>
 * Binding all GUI class listener in one class here.
 * @ author Robbie
 * @ version 1.0 2008-11-20
 * Version Modifier        Date        Description
 * 0.1     Robbie          2008-10-10  Initial version
 * 1.0     Piyusha Desai   2008-11-20 Cycle 2 Panel added
 */

public class Presentation extends JFrame implements ActionListener, PropertyChangeListener
{
	//All panel variable declare here.
	private SystemMain mainPanel;
	private SystemShutDown shutDownPanel;
	private SystemStart startPanel;
	private SystemSummary summaryPanel;
	private Receipt receiptPanel;
	private SystemReturns returnPanel ;
	private SystemReports reportPanel;
	
	
	//Declare and Initialize Listeners
	private EventListenerList listenerList = new EventListenerList();
	private ActionListener forwardListener = new ActionListener()
	{
		@Override
		public void actionPerformed(ActionEvent e) 
		{
			Object[] currentListeners = listenerList.getListenerList();
			
			for (int i = currentListeners.length - 2; i >= 0; i -= 2) {
				if (currentListeners[i] == ActionListener.class) {
					((ActionListener) currentListeners[i + 1]).actionPerformed(e);
				}
			}
		}
	};
	
	/**
	 * Constructor of Class 
	 * Add listener to each panel
	 */
	public Presentation()
	{
		mainPanel = new SystemMain(this);
		mainPanel.addActionListener(forwardListener);
		mainPanel.addActionListener(this);
		
		shutDownPanel = new SystemShutDown(this);
		
		startPanel = new SystemStart(this);
		startPanel.addActionListener(forwardListener);
		startPanel.addActionListener(this);
		
		summaryPanel = new SystemSummary(this);
		summaryPanel.addActionListener(forwardListener);
		summaryPanel.addActionListener(this);
		
		receiptPanel = new Receipt(this);
		receiptPanel.addActionListener(forwardListener);
		receiptPanel.addActionListener(this);
		
		returnPanel  = new SystemReturns(this);
		returnPanel .addActionListener(forwardListener);
		returnPanel .addActionListener(this);


		reportPanel = new SystemReports(this);
		reportPanel .addActionListener(forwardListener);
		reportPanel .addActionListener(this);

		this.setSize (550, 200);
		this.centerWindow(this);
		this.add(startPanel);
	}
	
	/**
	 * Add all listener into a list of listeners.
	 * @param l is a action listener from GUI.
	 */
	public void addActionListener(ActionListener l)
	{
		listenerList.add(ActionListener.class, l);
	}

    /**
     * Receiving Property change events from all GUI classes.
     *  
     */
	@Override
	public void propertyChange(PropertyChangeEvent evt)
	{
		mainPanel.propertyChange(evt);
		summaryPanel.propertyChange(evt);
		receiptPanel.propertyChange(evt);		
	}
	
	/**
	 * Method to set position of panel
	 */
	private void centerWindow(Window w)
	{		
		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension d = tk.getScreenSize();
		this.setLocation((d.width-w.getWidth())/2, (d.height-w.getHeight())/2);
	}

    /**
     * Method to fire action events
     */
	@Override
	public void actionPerformed(ActionEvent evt)
	{
		if(evt.getActionCommand().equals(ActionConstants.SYSTEM_INITIALIZE))
		{
			this.remove(startPanel);
			this.dispose();
			
			this.setSize (550, 550);
			this.centerWindow(this);
			
			this.add(summaryPanel);
			this.setVisible(true);
		}// Start window close and summary windows comes up.
		else if(evt.getActionCommand().equals(ActionConstants.ACCEPT_INITIALIZE))
		{
			this.remove(summaryPanel);
			this.dispose();
			
			this.setSize (550, 550);
			this.centerWindow(this);
			
			this.add(mainPanel);
			this.setVisible(true);
		}// Summary window close and Main windows comes up.
		else if(evt.getActionCommand().equals("SummaryCancel"))
		{
			this.remove(summaryPanel);
			this.dispose();
			
			this.setSize (550, 550);
			this.centerWindow(this);
			
			this.add(startPanel);
			this.setVisible(true);
		}// Summary window close and Start windows comes up.
		else if(evt.getActionCommand().equals(ActionConstants.PRINT_RECEIPT))
		{
			this.remove(mainPanel);
			this.dispose();
			
			this.setSize (550, 550);
			this.centerWindow(this);
			
			this.add(receiptPanel);
			this.setVisible(true);
		}// Main window close and receipt windows comes up.
		else if(evt.getActionCommand().equals(ActionConstants.RETURN_RECEIPT))
		{
			this.remove(returnPanel);
			this.dispose();
			
			this.setSize (550, 550);
			this.centerWindow(this);
			
			this.add(receiptPanel);
			this.setVisible(true);
		}// Return window close and return receipt windows comes up.
		else if (evt.getActionCommand().equals(ActionConstants.REPORT))
		{
			this.remove(mainPanel);
			this.dispose();
			
			this.setSize (550, 550);
			this.centerWindow(this);
			
			this.add(reportPanel);
			this.setVisible(true);
		}// Main window close and report windows comes up.
		
	}	

}
